﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using Luna小店.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.UC.辅料
{
    public partial class Frm款式类别下增加辅料位置 : DevExpress.XtraEditors.XtraForm
    {
        public List<DTO辅料位置> dTOs = new List<DTO辅料位置>();
        public List<DTO辅料位置> dto辅料位置 = new List<DTO辅料位置>();
        public String id;
        public String partName;
        public String materialCatelogId;
        public String STYLE_CATEGORY;
        public Frm款式类别下增加辅料位置(List<DTO辅料位置> dto辅料位置,String materialCatelogId,String STYLE_CATEGORY)
        {
            this.materialCatelogId = materialCatelogId;
            this.STYLE_CATEGORY = STYLE_CATEGORY;
            this.dto辅料位置 = dto辅料位置;
            InitializeComponent();
            this.getAll位置();
        }
        #region 选择辅料位置
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料位置_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn辅料位置_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料位置View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit辅料位置.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.id = this.searchLookUpEdit辅料位置.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                this.partName = this.searchLookUpEdit辅料位置.Properties.View.GetRowCellValue(rowHandle, "partName").ToString();
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn辅料位置_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅料位置.ToolTip = null;
            searchLookUpEdit辅料位置.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料位置_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.partName;
        }

        #endregion

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            foreach(DTO辅料位置 dTO in this.dto辅料位置)
            {
                if (dTO.id.Equals(this.id))
                {
                    MessageBox.Show("重复添加");
                    return;
                }
            }
            RestCall.PostHttp(URL.接口url+"/acc/saveStyleCategoryDescription", FunctionHelper.JsonSerialization(new DTO保存辅料类别位置(new FunctionHelper().Uuid, materialCatelogId, this.STYLE_CATEGORY, this.id)));
            this.dto辅料位置.Add(new DTO辅料位置(this.id, this.partName));
            this.Close();
        }

        private void simpleButton取消_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void getAll位置()
        {
            this.dTOs = FunctionHelper.JsonDeserialization<List<DTO辅料位置>>(RestCall.httpGetMethod(URL.接口url+"/acc/getAccessoryPartAll"));
            this.searchLookUpEdit辅料位置.Properties.DataSource = this.dTOs;
        }
    }
}
